<template>
  <div class="exhibits-top">
    <div
      class="ex-img-left"
      :class="{
        'ex-img-pc': !store.isMobileType,
        'ex-img-wap': store.isMobileType,
      }"
    >
      <div class="ex-img">
        <img
          class="cover"
          v-if="props.info.images?.length > 1"
          :src="$filters.getLinkUrl(props.info.images[current]?.data + '?imageView2/0/w/1000/format/webp')"
          alt=""
          @click="viewImg(props.info.images[current]?.data)"
        />
        <img
          class="cover"
          v-else
          :src="$filters.getLinkUrl(props.info.coverUrl + '?imageView2/0/w/1000/format/webp')"
          alt=""
          @click="viewImg(props.info.coverUrl)"
        />
      </div>
      <!-- 缩略图 -->
      <!-- store.isMobileType -->
      <div
        class="doot-box"
        :class="{ 'doot-anim': props.info.images?.length <= 4, 'doot-box-wap': store.isMobileType }"
        v-if="props.info.images?.length > 1"
        @mousewheel="handlWheel"
        ref="doot"
      >
        <div class="dot-content" :id="'dot-parents' + index" v-for="(item, index) in props.info.images" :key="index">
          <div
            @click="current = index"
            class="doot-item"
            :class="{ 'doot-item-active': current === index, 'item-mg': index === 0 }"
          >
            <div class="shadown">
              <span v-if="item.type == -1">首页</span>
              <span v-else-if="item.type == 1">正视图</span>
              <span v-else-if="item.type == 2">俯视图</span>
              <span v-else-if="item.type == 3">侧视图</span>
              <span v-else-if="item.type == 4">全景图</span>
              <span v-else-if="item.type == 5">局部图</span>
              <span v-else-if="item.type == 6">底部图</span>
              <span v-else>未设置</span>
            </div>
            <img
              :src="$filters.getLinkUrl(item.data + '?imageView2/1/w/58/h/58/format/webp')"
              alt=""
              class="item-img"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 右侧描述 -->
    <div class="exhibits-desc">
      <div class="exhibits-name" v-if="props.info.name">{{ props.info.name }}</div>
      <div class="exhibits-parameter">
        <div class="parameter-item">
          <div class="parameter-key">展品类型 :</div>
          <div class="parameter-name overflow">
            {{ props.info.typeValue || '-' }}
          </div>
        </div>
        <div class="parameter-item">
          <div class="parameter-key">展品级别 :</div>
          <div class="parameter-name overflow">
            <span class="item-title2" v-if="props.info.levelValue == '一级展品'"> 一级展品 </span>
            <span class="item-title2" v-else-if="props.info.levelValue == '二级展品'"> 二级展品 </span>
            <span class="item-title2" v-else-if="props.info.levelValue == '三级展品'"> 三级展品 </span>
            <span class="item-title2" v-else>{{ props.info.levelValue || '-' }}</span>
          </div>
        </div>
        <div class="parameter-item">
          <div class="parameter-key">展品来源 :</div>
          <div class="parameter-name overflow">
            {{ props.info.specificSources || '-' }}
          </div>
        </div>
        <div class="parameter-item">
          <div class="parameter-key">所属年代 :</div>
          <div class="parameter-name overflow">
            {{ props.info.concreteTimes ? props.info.concreteTimes : props.info.ageValue || '-' }}
          </div>
        </div>
        <div class="parameter-item">
          <div class="parameter-key">收藏单位 :</div>
          <div class="parameter-name overflow">
            {{ props.info.location ? props.info.location : info.companyName }}
          </div>
        </div>
        <div class="parameter-item">
          <div class="parameter-key">展品质地 :</div>
          <div class="parameter-name overflow">
            {{ props.info.texture || '-' }}
          </div>
        </div>
        <div class="parameter-item">
          <div class="parameter-key">展品尺寸 :</div>
          <div class="parameter-name overflow">
            {{ props.info.size || '-' }}
          </div>
        </div>
      </div>
      <div class="btn" v-if="info.openStatus === 1 && !store.isMobileType">
        <!-- 收藏展品 -->
        <!-- <t-popup
          v-if="info.openStatus === 1 && !store.isMobileType"
          attach="#template3-box"
          overlayClassName="popup"
          content="将此展品添加到我的关注"
          placement="top"
          destroyOnClose
        > -->
        <t-tooltip content="将此展品添加到我的关注" v-if="info.openStatus === 1 && !store.isMobileType">
          <div class="add-btn1 myPostattention" @click="postattention" v-if="info.openStatus === 1">
            <p v-if="!info.isFollow" class="iconfonts">&#xe6aa;</p>
            <p v-else class="iconfonts" style="color: #3586f3">&#xe6a9;</p>
            <!-- <div>{{ info.followTotal }}</div> -->
            <div style="color: #3586f3">{{ info.followTotal >= 1 ? info.followTotal : '收藏展品' }}</div>
          </div>
        </t-tooltip>
        <!-- </t-popup> -->

        <!-- 添加到展览 -->
        <!-- <t-popup
          v-if="info.openStatus === 1 && !store.isMobileType"
          attach="#template3-box"
          overlayClassName="popup"
          content="将此展品添加到我的展览"
          placement="top"
          destroyOnClose
        > -->
        <t-tooltip content="将此展品添加到我的展览" v-if="info.openStatus === 1 && !store.isMobileType">
          <div
            class="add-btn2"
            :style="{ backgroundImage: `url(${publicUrls + 'free/style14/addIcon.png?imageView2/format/webp'})` }"
          >
            <div @click="addId" class="add-exhibition">
              <span class="iconfonts">&#xe6a7;</span>
              <span>添加至展览</span>
            </div>
            <!-- <div class="add-line"></div> -->
          </div>
        </t-tooltip>
      </div>
    </div>
    <!-- 添加展览弹窗 -->

    <!-- 是否登录 -->
    <messageModel
      submitText="前往"
      title="提示"
      :visible="visibleMessage"
      problem="您暂未登录"
      message="是否前往登录？"
      @closeClick="closeClick"
      @submitClick="pathSubmitClick"
      style="z-index: 999"
    />
  </div>
</template>

<script lang="ts">
export default defineComponent({
  name: 'exhibitsTop',
});
</script>
<script lang="ts" setup>
// @ts-nocheck
import { defineComponent, ref, computed, watch } from 'vue';
import { projectLists } from '@/api/project/project';
import { MessagePlugin } from 'tdesign-vue-next';
import { getHeardToken } from '@/api/utils';
import { addExhibits } from '@/api/desgin/desgin';
import { publicUrls } from '@/utils/minxin';
import { followSet } from '@/api/interact/interact';
import { useProjectStore } from '@/store';
import { ElMessage, ElMessageBox } from 'element-plus';
import type { Action } from 'element-plus';

const visibleMessage = ref(false);
const selectInitList = ref();
const projectId = ref(null);
const current = ref(0);
const store = useProjectStore();
const loading = ref(false);
const doot = ref(null);
const visibleStep = ref(false);
const Scrollheights = ref();

let viewportHeight = window.innerHeight;
Scrollheights.value = viewportHeight * 0.5844;
const getScrollHeight = computed(() => {
  let rootNode = document.documentElement;

  // 获取计算后的样式
  let computedStyle = window.getComputedStyle(rootNode);

  // 读取字体大小（注意这里返回的可能是一个包含单位的字符串，如"16px"）
  let fontSize = computedStyle.fontSize;

  // 如果你需要一个数字值（不带单位）进行计算，你可能需要解析这个字符串
  // 例如，使用正则表达式或split()方法去除单位，并将结果转换为数字
  let fontSizeNumber = parseFloat(fontSize.replace(/[^0-9.-]/g, ''));
  let heights = (fontSizeNumber / 144) * 512;
  return heights + 'px';
});
const props = defineProps({
  info: {
    type: Object,
  },
});

const emits = defineEmits(['viewImg', 'addExId']);

const viewImg = (i: any) => {
  emits('viewImg', i, current.value);
};

// 添加关注事件
const postattention = async () => {
  let token = getHeardToken();
  if (!token) {
    store.loginModeShow = true;

    return;
  } else {
    try {
      let { data } = await followSet({ refType: 4, refId: props.info.id });
      props.info.isFollow = data;
      if (props.info.isFollow) {
        props.info.followTotal = props.info.followTotal + 1;
        MessagePlugin.success('关注成功');
      } else {
        props.info.followTotal = props.info.followTotal === 0 ? 0 : props.info.followTotal - 1;
        MessagePlugin.warning('取消关注');
      }
    } catch {}
  }
};

//获取账号项目
const selectInit = async () => {
  try {
    let res = await projectLists(props.info.id);
    if (res.data.length > 0) {
      selectInitList.value = res.data;
      visibleStep.value = true;
      console.log(selectInitList.value);
    } else MessagePlugin.warning('暂无可添加的展览');
  } catch (err) {
    console.log(err);
  }
};
const setCurrent = (index: number) => {
  current.value = index;
  let scrollContainer = doot.value;
  const element = document.getElementById('dot-parents' + index);
  const rect = element.getBoundingClientRect();
  const containerRect = scrollContainer.getBoundingClientRect();
  const offsetleft = index > 2 ? rect.width * index - rect.width : 0; //rect.left - containerRect.left + scrollContainer.scrollLeft;

  // 设置滚动容器的scrollTop
  scrollContainer.scrollLeft = offsetleft;
  // dot-parents
};
// 添加到展览
const addId = () => {
  let token = getHeardToken();
  if (!token) {
    // ElMessageBox.alert('是否前往登录？', '您暂未登录', {
    //   confirmButtonText: '确定',
    //   callback: (action: Action) => {
    //     if (action === 'confirm') {
    //       pathSubmitClick();
    //     }
    //   },
    // });
    // visibleMessage.value = true;
    store.loginModeShow = true;
    return;
  }
  // selectInit();
  emits('addExId');
};

const closetables = (context?: any): void => {
  visibleStep.value = false;
};

//添加项目
const okStep = async () => {
  try {
    if (loading.value) {
      return;
    }
    if (projectId.value) {
      loading.value = true;
      let res = await addExhibits(projectId.value, props.info.id, [props.info.id]);
      if (res.succeeded) {
        MessagePlugin.success('添加成功');
      }

      loading.value = false;
      visibleStep.value = false;
      projectId.value = null;
    } else {
      MessagePlugin.success('请选择展览');
      return;
    }
  } catch {
    loading.value = false;
  }
};

//取消
const closeClick = () => {
  visibleMessage.value = false;
};
//确认登录
const pathSubmitClick = () => {
  visibleMessage.value = false;
  let url = window.location.href;
  let redirectUrl = 'http://wz.wuzhan.art/#/login?redirect=' + url;
  window.location.href = redirectUrl;
};

const open3DUrl = () => {
  if (!props.info.externalUrl) return;
  window.open(props.info.externalUrl);
};

// 鼠标横向滚动事件
const handlWheel = (event: any) => {
  const delta = event.wheelDelta || -event.detail;
  doot.value.scrollLeft -= delta;
  event.preventDefault();
};

watch(
  () => props.info,
  () => {
    current.value = 0;
  },
);
</script>

<style lang="less" scoped>
.exhibits-top {
  width: 100%;
  flex-shrink: 0;
  display: flex;
  overflow: hidden;
  min-height: 480px;
}
.ex-img-left {
  flex-shrink: 0;
  width: 50%;
  height: 480px;
  position: relative;
  cursor: pointer;
}
.ex-img {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #e8e1d4;
}
.cover {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: all 0.5s;
}
.ex-img-left:hover {
  .cover {
    transform: scale(1.2);
  }
}
.ex-img-wap {
  position: relative;
}
.ex-img-pc {
  position: relative;
}
.doot-box {
  width: 276px;
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  overflow: auto;
  scrollbar-width: none;
  &::-webkit-scrollbar {
    display: none;
  }
}
.doot-anim {
  justify-content: center;
}
.dot-content {
  flex-shrink: 0;
  width: 60px;
  height: 60px;
  border-radius: 4px;
  margin-right: 12px;
  // margin-bottom: 12px;
}
.dot-content:last-child {
  margin-right: 0;
}
.shadown {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #333;
  border-radius: 4px;
  opacity: 0.6;
  span {
    color: #fff;
    font-size: 16px;
  }
}
.doot-item {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 4px;
}
.doot-item:hover {
  .shadown {
    z-index: 3;
  }
}
.doot-item-active {
  .shadown {
    z-index: 3;
  }
}
.item-img {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid #3586f3;
}
.exhibits-desc {
  width: calc(100% - 50%);
  margin-left: 30px;
}
.exhibits-name {
  width: 100%;
  color: #fff;
  font-family: 'SourceHanSerifCN';
  font-size: 28px;
  font-style: normal;
  font-weight: 700;
  line-height: 180%; /* 50.4px */
}
.exhibits-parameter {
  margin-top: 30px;
}
.parameter-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 16px;
}
.parameter-item:last-child {
  margin-bottom: 0;
}
.parameter-key {
  flex-shrink: 0;
  color: #fff;
  text-align: center;
  font-family: 'PingFang SC';
  font-size: 15px;
  font-style: normal;
  font-weight: 400;
  line-height: 32px; /* 213.333% */
  opacity: 0.6;
  white-space: nowrap;
}
.parameter-name {
  flex-shrink: 0;
  color: #fff;
  font-family: 'Microsoft YaHei';
  font-size: 17px;
  font-style: normal;
  font-weight: 400;
  line-height: 32px; /* 188.235% */
  margin-left: 16px;
  max-width: 360px;
}
.btn {
  width: 100%;
  display: flex;
  align-items: center;
  margin-top: 32px;
}
.myPostattention {
  width: 64px;
  height: 48px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  p {
    color: #3586f3;
    text-align: center;
    font-family: 'PingFang SC';
    font-size: 16px;
    font-style: normal;
    font-weight: 400;
    line-height: 24px; /* 150% */
  }
}
.add-btn2 {
  width: 388px;
  height: 48px;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 18px;
  cursor: pointer;
}
.add-exhibition {
  display: flex;
  align-items: center;
  color: #3586f3;
  text-align: center;
  font-family: 'PingFang SC';
  font-size: 16px;
  font-style: normal;
  font-weight: 400;
  line-height: 24px; /* 150% */
  .iconfonts {
    margin-right: 4px;
    font-size: 24px;
  }
}
</style>
